package sza.oracle.apps.ap.oie.budget.server;

import java.sql.SQLException;

import java.util.ArrayList;

import oracle.apps.fnd.framework.OAException;
import oracle.apps.fnd.framework.OAViewObject;
import oracle.apps.fnd.framework.server.OAApplicationModuleImpl;
import oracle.apps.fnd.framework.server.OADBTransaction;

import oracle.jbo.Row;

import oracle.jdbc.OracleCallableStatement;
import oracle.jdbc.OracleTypes;

import sza.oracle.apps.ap.oie.poimport.server.AllOrgnazitionVOImpl;
import sza.oracle.apps.ap.oie.poimport.server.ExpenseTemplateVOImpl;
// ---------------------------------------------------------------------
// ---    File generated by Oracle ADF Business Components Design Time.
// ---    Custom code may be added to this class.
// ---    Warning: Do not modify method signatures of generated methods.
// ---------------------------------------------------------------------
public class SzaOieBudgetAMImpl extends OAApplicationModuleImpl {
    /**This is the default constructor (do not remove)
     */
    public SzaOieBudgetAMImpl() {
    }

    /**Container's getter for SzaOiePaExpenseMappingVO1
     */
    public SzaOiePaExpenseMappingVOImpl getSzaOiePaExpenseMappingVO1() {
        return (SzaOiePaExpenseMappingVOImpl)findViewObject("SzaOiePaExpenseMappingVO1");
    }

    /**Sample main for debugging Business Components code using the tester.
     */
    public static void main(String[] args) {
        launchTester("sza.oracle.apps.ap.oie.budget.server", /* package name */
      "SzaOieBudgetAMLocal" /* Configuration Name */);
    }
    
    public void initQuery(){
        SzaOiePaExpenseMappingVOImpl vo = this.getSzaOiePaExpenseMappingVO1();
        vo.executeQuery();
    }

    /**Container's getter for ImportFileVO1
     */
    public ImportFileVOImpl getImportFileVO1() {
        return (ImportFileVOImpl)findViewObject("ImportFileVO1");
    }
    
    public void importVmi(String[] csvline,String blobId) {
              int index        = 0;
              int segmentIndex = 0;
              int anchorFrom   = 0;
              int anchorTo     = 0;
              boolean validateFlag  = true;
              String[] csvSegment   = new String[20];
              String[] errbuf       = new String[1];
              String[] retcode      = new String[1]; 
              ArrayList alistErr = new ArrayList(2000);
           
//              ImportFileVOImpl fileVO = getImportFileVO1();
              //get batch id from sequence
//              String batch_id = String.valueOf(getOADBTransaction().getSequenceValue("CUX_CSV_IMPORT_BATCH_S"));
//              getOADBTransaction().putTransientValue("batchId",batch_id);
              //break the context by the sign ","
              while (csvline[index] != null) {
                  anchorFrom = 0;
                  anchorTo = 0;
                  segmentIndex = 0;
                  validateFlag = true;
                  anchorTo = csvline[index].indexOf(",",anchorFrom);
                  while (anchorTo != -1) {
                      csvSegment[segmentIndex] = (csvline[index].substring(anchorFrom,anchorTo));
                      anchorFrom = anchorTo + 1;
                      anchorTo = csvline[index].indexOf(",",anchorFrom);
                      segmentIndex++;
                  }
                 
                  csvSegment[segmentIndex] = csvline[index].substring(anchorFrom,csvline[index].length());
               
               
                  if(validateFlag){
                      try{
                          insertVmiTrx(   
                                          csvSegment[0],
                                          csvSegment[1],
                                          csvSegment[2],
                                          csvSegment[3],
                                          csvSegment[4],
                                          csvSegment[5],
                                          csvSegment[6],
                                          csvSegment[7],
                                          csvSegment[8],
                                          errbuf,
                                          retcode);
                      }catch(SQLException se){
                          throw new OAException(se.getMessage()+"this is a test");
                      }
                      if(!("0".equals(retcode[0])))
                      {
                          alistErr.add(new OAException("Row "+(index+1)+": "+errbuf[0], OAException.ERROR));
                      }
                  }            
                  index++;
                  getTransaction().commit();
              }
          }
          public void insertVmiTrx(
                                    String segment1,
                                    String segment2,
                                    String segment3,
                                    String segment4,
                                    String segment5,
                                    String company,
                                    String dept,
                                    String paymentType,
                                    String paymentProject,
                                    String [] errbuf,
                                    String [] retcode)  
          throws SQLException {
              OADBTransaction txn = getOADBTransaction();
              OracleCallableStatement ocs = null;
              try {
                  ocs = (OracleCallableStatement)txn.createCallableStatement("BEGIN SZA_OIE_BUDGET_UTIL.import_coa_expense_mapping"+
                                     "(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11);end;",1);
                  ocs.setString(1,segment1);
                  ocs.setString(2,segment2);
                  ocs.setString(3,segment3);
                  ocs.setString(4,segment4);
                  ocs.setString(5,segment5);
                  ocs.setString(6,company);
                  ocs.setString(7,dept);
                  ocs.setString(8,paymentType);
                  ocs.setString(9,paymentProject);
                  ocs.registerOutParameter(10,OracleTypes.VARCHAR);
                  ocs.registerOutParameter(11,OracleTypes.VARCHAR);
                  ocs.execute();
                  if (errbuf != null) {
                      errbuf[0] = ocs.getString(10);
                  }
                  if (retcode != null) {
                      retcode[0] = ocs.getString(11);
                  }
                  if("2".equals(ocs.getString(11))){
                      throw new OAException(ocs.getString(10));
                  }
              }
              catch(SQLException Se) {
                  throw new OAException(Se.getMessage());
              } 
              finally {
                   if(ocs!=null)
                   try{
                      //关闭声明
                      ocs.close();
                  }
                  catch(SQLException Se)
                  {
                      throw OAException.wrapperException(Se);
                  }
              }           
          }

    /**Container's getter for SearchVO1
     */
    public SearchVOImpl getSearchVO1() {
        return (SearchVOImpl)findViewObject("SearchVO1");
    }
    
    public void search(){
        SzaOiePaExpenseMappingVOImpl vo = this.getSzaOiePaExpenseMappingVO1();
        SearchVOImpl searchVo = this.getSearchVO1();
        Row row = searchVo.getCurrentRow();
        vo.search((String)row.getAttribute("CompanyCode"),(String)row.getAttribute("CostCenterCode"),(String)row.getAttribute("AccountCode"),(String)row.getAttribute("SubAccountCode"),(String)row.getAttribute("ReferenceCode"));
    }

    /**Container's getter for SzaOieMappingMaintainVO1
     */
    public SzaOiePaExpenseMappingVOImpl getSzaOieMappingMaintainVO1() {
        return (SzaOiePaExpenseMappingVOImpl)findViewObject("SzaOieMappingMaintainVO1");
    }

    /**Container's getter for AllOrgnazitionVO1
     */
    public AllOrgnazitionVOImpl getAllOrgnazitionVO1() {
        return (AllOrgnazitionVOImpl)findViewObject("AllOrgnazitionVO1");
    }

    /**Container's getter for ExpenseTemplateVO1
     */
    public ExpenseTemplateVOImpl getExpenseTemplateVO1() {
        return (ExpenseTemplateVOImpl)findViewObject("ExpenseTemplateVO1");
    }
    
    public void createEmptyEntity(){
        OAViewObject vo = (OAViewObject)getSzaOieMappingMaintainVO1();
        if (!vo.isPreparedForExecution()) {
            vo.executeQuery();
            Row newRow = vo.createRow();
            newRow.setNewRowState((byte)-1);
            vo.insertRow(newRow);        
        }
    }
    
    public void getMappingEntity(String mappingId){
        SzaOiePaExpenseMappingVOImpl vo = (SzaOiePaExpenseMappingVOImpl)getSzaOieMappingMaintainVO1();
        vo.query(mappingId);
    }

    /**Container's getter for CompanyLovVO1
     */
    public CompanyLovVOImpl getCompanyLovVO1() {
        return (CompanyLovVOImpl)findViewObject("CompanyLovVO1");
    }
}
